#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module to initialize execute environment for TaggyTODO. When TaggyTODO
is launched (UI or command line), it requires some configurations to
make sure application can run correctly. Right now we have the following
configurations:

- Debugging flags
- Default database files
"""

__all__ = [ 'Config' ]

import os
import tatolib.errors as errors

ENVVAR_DEBUG     = "TATO_DEBUG"
ENVVAR_DBFILE    = "TATO_DBFILE"
DEFAULT_TATOPATH = ".tato"
DEFAULT_DBFILE   = "tato.db"

class Config(object):
    def __init__(self):
        "Constructor."
        self.__get_dbgflag()
        self.__get_dbfile()
        pass
    def __get_dbgflag(self):
        self.__debug = False
        if os.getenv(ENVVAR_DEBUG) is not None:
            self.__debug = True

    def __get_dbfile(self):
        dbfile = os.getenv(ENVVAR_DBFILE)
        if dbfile is not None:
            tatopath = os.path.dirname(dbfile)
            if len(tatopath) is 0:
                # No path name, the file is under current directory. 
                tatopath = "."
        else:
            # No environment specified. Fallback to ~/.tato/tato.db 
            homepath = os.getenv("HOME")
            tatopath = os.path.join(homepath, DEFAULT_TATOPATH)
            dbfile = os.path.join(tatopath, DEFAULT_DBFILE)
        try:
            if not os.path.exists(tatopath):
                os.makedirs(tatopath)
        except Exception:
            raise errors.DatabaseError(tatopath, errors.E_DB_WRONGPATH)

        if os.path.exists(dbfile) and not os.path.isfile(dbfile):
            raise errors.DatabaseError(dbfile, errors.E_DB_NOTFILE)
        self.__dbfile = dbfile

    @property
    def debug(self):
        """
        Property debug: True if debugging environment variable is
        set, or False if it's not set.
        """
        return self.__debug
    @property
    def dbfile(self):
        "Property dbfile: Path of database file."
        return self.__dbfile

# vim:tw=78:et:sw=4
